package org.standard.commons.struts2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.standard.commons.model.Annex;
import org.standard.commons.service.Service;
import org.standard.commons.tlds.PropertiesFileConstants;
import org.standard.commons.utils.Utils;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 */
public abstract class BaseAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware, SessionAware {
   
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected Map session;
	private String downName;
	
	// 预定义逻辑视图常量
	public final static String CREATE = "create";
	public final static String LIST = "list";
	public final static String LIST_READ = "list_read";
	public final static String MODIFY = "modify";
	public final static String DETAIL = "detail";
	public final static String DETAIL_READ = "detail_read";
	public final static String INDEX = "index";
	public final static String CREATE_SUCCESS = "create_success";
	public final static String MODIFY_SUCCESS = "modify_success";
	public final static String DELETE_SUCCESS = "delete_success";
	public final static String QUERY = "query";
	public final static String JSON = "json";
	
	public static boolean isRead = false;//用于查询列表的时候是否只能读
	
	private String ERROR;//错误提示信息
	private String hintType;//错误提示信息类型
	//传到页面的提示信息
	public String info = "";
	private Annex annexObj;
	protected Service service;

	protected String objToString(Object obj) {
		if (obj == null) {
			return "";
		}
		return obj.toString();
	}
	protected String objToCurrency(Object obj) {
		if (obj == null) {
			return "0.00";
		}
		if (obj instanceof Double) {
			Double temp = (Double) obj;
			return new BigDecimal(temp).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
		}
		return obj.toString();
	}
	protected String objToCurrencyFormat(Object obj) {
		
		String currency = objToCurrency(obj);
		String temp[] = currency.split("\\.");
		if (temp.length == 1) {
			return currency;
		}
		if (Long.parseLong(temp[1]) == 0) {
			return temp[0];
		}
		return currency;
	}
	protected String getPropertiesValue(String optionName, String value) {
		
		return getPropertiesValue(optionName, value, false);
	}
	
	protected String getPropertiesValue(String optionName, String value, boolean isSkipTop) {
		
		PropertiesFileConstants props = PropertiesFileConstants.getPropertiesFileConstants();
		String propsValue = props.get(optionName).toString(); 
		String propsValueArray [] = propsValue.split(",");
		
		String outString = "";
		int j = 0;
		if (isSkipTop) {
			j = 1;
		}
		for(int i = j; i < propsValueArray.length; i++){
			String nameValueArray [] = propsValueArray[i].split(":");
			if(!Utils.isStringNull(value) && nameValueArray[1].trim().equals(value)){
				outString = nameValueArray[0].trim();
			}
		}
		return outString;
	}
	protected int getPropertiesLength(String optionName) {
		
		PropertiesFileConstants props = PropertiesFileConstants.getPropertiesFileConstants();
		Object obj = props.get(optionName);
		if (obj == null) {
			return 0;
		}
		String propsValue = obj.toString(); 
		String propsValueArray [] = propsValue.split(",");
		return propsValueArray.length;
	}

	@JSON(name="info")
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Map getSession() {
		return session;
	}

	public String getERROR() {
		return ERROR;
	}

	public void setERROR(String error) {
		ERROR = error;
	}
	
	public String getHintType() {
		return hintType;
	}
	
	public void setHintType(String hintType) {
		this.hintType = hintType;
	}
	//////////////////////////////////////下载文件共用方法//////////////////////////////
	public InputStream getInputPath() throws FileNotFoundException {
		
		annexObj = (Annex) service.findPOJOById(Annex.class, annexObj.getAnnexId());
		setDownName(annexObj.getAnnexName());
		return new FileInputStream(annexObj.getAnnexPath());
	}
	public String download() {
		
		return SUCCESS;
	}
	/**
	 * 对下载文件的中文名进行编码
	 * @return
	 * @throws UnsupportedEncodingException
	 */
    public String getDownName() throws UnsupportedEncodingException { 
    	downName = new String(downName.getBytes(),"ISO-8859-1"); 
        return downName; 
    }
	public void setDownName(String downName) {
		this.downName = downName;
	}
	public Annex getAnnexObj() {
		return annexObj;
	}
	public void setAnnexObj(Annex annexObj) {
		this.annexObj = annexObj;
	}
	public void setService(Service service) {
		this.service = service;
	}


}
